package com.ruoyi.web.controller.Util;


import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Objects;

public class ImageToBase64Utils {
    private static String strNetImageToBase64;
    static String sc = "";


    /**
     * 网络图片转换Base64的方法
     *
     * @param netImagePath
     */
    public static String NetImageToBase64(String netImagePath) {

        final ByteArrayOutputStream data = new ByteArrayOutputStream();
        try {
            // 创建URL
            URL url = new URL(netImagePath);
            final byte[] by = new byte[1024];
            // 创建链接
            final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);

            try {
                InputStream is = conn.getInputStream();
                // 将内容读取内存中
                int len = -1;
                while ((len = is.read(by)) != -1) {
                    data.write(by, 0, len);
                }
                // 对字节数组Base64编码
//                        BASE64Encoder encoder = new BASE64Encoder();
//                        strNetImageToBase64 = encoder.encode(data.toByteArray());
                strNetImageToBase64 = Base64.encodeBase64String(data.toByteArray());
//                        System.out.println("网络图片转换Base64:" + strNetImageToBase64);
                sc = strNetImageToBase64;
                // 关闭流
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }


        } catch (IOException e) {
            e.printStackTrace();
            return "";
        } finally {
//            return strNetImageToBase64;
            return sc;
        }
    }

    public static String myImageToBase64(String imgPath) {
        byte[] data = null;
        // 读取图片字节数组
        try {
            InputStream in = new FileInputStream(imgPath);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
//        BASE64Encoder encoder = new BASE64Encoder();
        // 返回Base64编码过的字节数组字符串
//        return encoder.encode(Objects.requireNonNull(data));
        return Base64.encodeBase64String(Objects.requireNonNull(data));
    }

}
